package swing;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.util.List;

import javax.swing.JButton;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import lib.InteractiveForm;
import lib.InteractiveTableModel;
import lib.WebsiteTableModel;



import dao.WebsiteDAO;

import pojo.Website;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class WebsiteTable extends JPanel {
	private JTable table;
	private WebsiteTableModel tableModel;
	private static JTabbedPane tabPane=null;
	private JButton btActive;
	private static final String[] columnNames = {
		"WebsiteID", "Website", "Menu1", "Menu2", "Page", "WebContent", "Image", "BookContent", "BookInfo", "WebType","Active"
	};
	//protected 	List<Website> listWeb;
	/**
	 * Create the panel.
	 */
	public WebsiteTable(JTabbedPane tabbedPane) {
		
		this.tabPane=tabbedPane;
		setLayout(null);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		double width = screenSize.getWidth();
		double height = screenSize.getHeight();
		tableModel = new WebsiteTableModel(columnNames);
	
		table = new JTable(tableModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int row= table.getSelectedRow();
				if(row>-1)
				{
					Website w= tableModel.getRow(row);
					if(w.getActive()==null||w.getActive().equals("false")) btActive.setText("Activate");
					else btActive.setText("Deactivate");
				}
			}
		});
		table.setPreferredScrollableViewportSize(new Dimension((int) (width-200),(int)height-200));
		table.setFillsViewportHeight(true);

		TableColumnModel tcm = table.getColumnModel();
		tcm.getColumn(10).setPreferredWidth(12);
		tcm.getColumn(0).setPreferredWidth(12);
		JScrollPane scroll= new JScrollPane(table);
		scroll.setBounds(6, 5, (int) (width-120), (int)height-100);
		add(scroll);
		
		JButton btXoa = new JButton("Delete");
		btXoa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n= table.getSelectedRow();
				if(n==-1)
				{
					JOptionPane.showMessageDialog (null,"Vui long chon dong muon xoa");
					return;
				}
				else
				{	
					tableModel.removeRow(n);
					//listWeb.remove(n);	
				}
			}
		});
		btXoa.setBounds(1266, 149, 80, 41);
		
		add(btXoa);
		
		JButton btThem = new JButton("New");
		btThem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabPane.setSelectedIndex(1);
			}
		});
		btThem.setBounds((int) (width-100), 41, 80, 41);
		add(btThem);
		
		btActive = new JButton("Activate");
		btActive.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n= table.getSelectedRow();
				if(n==-1)
				{
					JOptionPane.showMessageDialog (null,"Vui long chon website muon thay doi");
					return;
				}
				else
				{	
					tableModel.updateActiveion(n);
				}
				
			}
		});
		btActive.setToolTipText("K\u00EDch ho\u1EA1t hay h\u1EE7y 1 trang web");
		btActive.setBounds(1266, 201, 80, 41);
		add(btActive);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableModel.saveToDatabase();
				JOptionPane.showMessageDialog(null,"Save du lieu thanh cong");
			}
		});
		btnSave.setToolTipText("L\u01B0u s\u1EF1 thay \u0111\u1ED5i v\u00E0o database");
		btnSave.setBounds(1266, 253, 80, 41);
		add(btnSave);
		
		JButton btnUpdate = new JButton("Refresh");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableModel.recoveryData();
			}
		});
		btnUpdate.setToolTipText("C\u1EADp nh\u1EADt l\u1EA1i c\u1EDF s\u1EDF d\u1EEF li\u1EC7u t\u1EEB database");
		btnUpdate.setBounds(1266, 305, 80, 41);
		add(btnUpdate);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n= table.getSelectedRow();
				if(n==-1)
				{
					JOptionPane.showMessageDialog (null,"Vui long chon website muon thay doi");
					return;
				}
				else
				{	
					WebsiteGUI tab2= (WebsiteGUI) tabPane.getComponent(1);
					tab2.setInfomation(tableModel.getRow(n));
					tab2.setUrl(tableModel.getRow(n).getWebsite());
					tabPane.setSelectedIndex(1);
				}
			}
		});
		btnEdit.setBounds(1266, 93, 80, 41);
		add(btnEdit);
	}
	
	public Object[][] convert(List<Website> l)
	{
		Object[][] data=  (Object[][]) new Object[50][11];

		for(int i= 0;i<l.size();i++)
		{
			Website web= l.get(i);
			data[i][0]=web.getWebsiteId();
			data[i][1]=web.getWebsite();
			data[i][2]=web.getMenu1();
			data[i][3]=web.getMenu2();
			data[i][4]=web.getPage();
			data[i][5]=web.getWebContent();
			data[i][6]=web.getImage();
			data[i][7]=web.getBookContent();
			data[i][8]=web.getBookInfo();
			data[i][9]=web.getWebType();
			data[i][10]=web.getActive();
		}
		return data;
	}
	
	/*public class InteractiveTableModelListener implements TableModelListener{

		@Override
		public void tableChanged(TableModelEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
	}*/
}
